package com.example.myprojectoffeixue;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.localization.Gateway;
import com.localization.GatewayRequestUtil;
import com.localization.SendUtil;

import org.apache.http.entity.StringEntity;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

public class StudyKTActivity extends Activity implements OnClickListener {
    //学习空调遥控器
    private HongWaiSB h;
    private MyApplication m;
    private boolean flag1;
    private EditText et;
    private LinearLayout l;
    private ImageView iv1;
    private ImageView iv2;
    private ImageView iv3;
    public ImageView tv;

    public TextView tv1;

    public LinearLayout tv2;

    public LinearLayout tv3;

    public TextView tv4;

    public TextView tv5;

    public TextView tv6;

    public TextView tv7;

    public ImageView tv10;

    public ImageView tv11;

    public LinearLayout tv14;
    public TextView tv15;
    public LinearLayout tv16;
    public TextView tv17;
    private String s;
    private AlertDialog.Builder builder;
    public String code = "25";
    public String mode = "02";
    public String flag2 = "00";
    public String speed = "01";
    public int id;
    LinearLayout l1;

    Handler hand = new Handler() {

        public void handleMessage(android.os.Message msg) {

            if (msg.what == 2) {

                HttpConnection.showToast(getBaseContext(),"学习成功");

            }
        }

        ;
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study_kt);
        info();

    }

    private void info() {
        LinearLayout iv = (LinearLayout) findViewById(R.id.iv_kongtiao);
        iv.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();

            }
        });

        Intent it = getIntent();
        s = it.getStringExtra("num");
        id = it.getIntExtra("id", -1);
        String flag = it.getStringExtra("flag");
        if (flag != null && "a".equals(flag)) {
            flag1 = true;

        }
        m = (MyApplication) getApplication();
        m.hand = hand;
        h = new HongWaiSB();
        h.cmd = "1012";
        h.extra = new Extra();

        h.status = "02";
        if (!flag1) {
            h.status = "01";
            h.extra.codeIndex = s;
        }

        h.extra.type = m.type;
        iv1 = (ImageView) findViewById(R.id.start_kongtiao);
        h.mac=m.mac_ykq;
        h.ieee = m.ieee_ykq;
        h.port = m.port_ykq;
        l1 = (LinearLayout) findViewById(R.id.lin_kongtiao);
        tv = (ImageView) findViewById(R.id.start_kongtiao);
        tv.setOnClickListener(this);
        iv2 = (ImageView) findViewById(R.id.dianjizhire);
        iv3 = (ImageView) findViewById(R.id.iv_lengfeng);

        tv2 = (LinearLayout) findViewById(R.id.lengfeng_kongtiao);
        tv2.setOnClickListener(this);

        tv3 = (LinearLayout) findViewById(R.id.nuanfeng_kongtiao);
        tv3.setOnClickListener(this);

        tv4 = (TextView) findViewById(R.id.zidongfeng_kongtiao);
        tv4.setOnClickListener(this);
        tv5 = (TextView) findViewById(R.id.digufeng_kongtiao);
        tv5.setOnClickListener(this);
        tv6 = (TextView) findViewById(R.id.zhongsu_kongtiao);
        tv6.setOnClickListener(this);
        tv7 = (TextView) findViewById(R.id.gaofeng_kongtiao);
        tv7.setOnClickListener(this);

        tv10 = (ImageView) findViewById(R.id.addwd_kongtiao);
        tv10.setOnClickListener(this);
        tv11 = (ImageView) findViewById(R.id.jswd_kongtiao);
        tv11.setOnClickListener(this);
        tv15 = (TextView) findViewById(R.id.wendu_kongtiao);
        Button bt = (Button) findViewById(R.id.a0baocun);
        bt.setOnClickListener(this);

        builder = new Builder(StudyKTActivity.this);
        builder.setMessage("请点击遥控器的对应按钮");

        builder.setTitle("提示");

        builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

                dialog.dismiss();

            }

        });
    }

    // {"cmd":"1012","ieee":"

    // 00124B000AF83E16","port":"08","status":"02",extra{"type":"04"}}

    @Override
    public void onClick(View v) {
        String l = "";

        switch (v.getId()) {
            // 空调开
            case R.id.start_kongtiao:
                m.AnNiuId = 1;

                if ("01".equals(flag2)) {
                    h.extra.powerStatus = "00";
                    flag2 = "00";
                    iv1.setImageResource(R.drawable.ktguan);
                } else {
                    h.extra.powerStatus = "01";
                    flag2 = "01";
                    iv1.setImageResource(R.drawable.start);
                }


                if (flag1) {
                    m.YuanSM.clear();
                    builder.show();
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    }
                } else {
                    h.extra.settingType = "01";
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    String localcmd=JSON.toJSONString(h.extra);
                    if (!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if(m.flag3){
                        m.c.send(l);}
                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr4(m.ieee_ykq,m.port_ykq,localcmd);
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                }
                break;
            // 制冷
            case R.id.lengfeng_kongtiao:
                m.AnNiuId = 1;
                mode = "02";
                iv3.setImageResource(R.drawable.dianjizhileng);
                iv2.setImageResource(R.drawable.zhire);

                if (flag1) {
                    m.YuanSM.clear();
                    builder.show();
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    }
                } else {

                    h.extra.settingType = "02";
                    h.extra.powerStatus = flag2;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    String localcmd=JSON.toJSONString(h.extra);
                    if (!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if(m.flag3){
                            m.c.send(l);}
                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr4(m.ieee_ykq,m.port_ykq,localcmd);
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                }

                break;
            // 制热
            case R.id.nuanfeng_kongtiao:
                m.AnNiuId = 1;
                mode = "05";
                iv2.setImageResource(R.drawable.dinajizhire);
                iv3.setImageResource(R.drawable.zhileng);

                if (flag1) {

                    m.YuanSM.clear();
                    builder.show();

                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    }

                } else {

                    h.extra.settingType = "02";
                    h.extra.powerStatus = flag2;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    String localcmd=JSON.toJSONString(h.extra);
                    if (!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if(m.flag3){
                            m.c.send(l);}
                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr4(m.ieee_ykq,m.port_ykq,localcmd);
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                }

                break;
            // 自动模式
            case R.id.zidongfeng_kongtiao:
                m.AnNiuId = 1;
                speed = "01";
                l1.setBackgroundResource(R.drawable.fengzidong);

                if (flag1) {
                    m.YuanSM.clear();
                    builder.show();
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    }


                } else {
                    h.extra.settingType = "03";
                    h.extra.powerStatus = flag2;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    String localcmd=JSON.toJSONString(h.extra);
                    if (!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if(m.flag3){
                            m.c.send(l);}
                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr4(m.ieee_ykq,m.port_ykq,localcmd);
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                }

                break;
            // 低速风
            case R.id.digufeng_kongtiao:
                m.AnNiuId = 1;
                speed = "02";
                l1.setBackgroundResource(R.drawable.disufeng);
                if (flag1) {
                    m.YuanSM.clear();
                    builder.show();
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    }


                } else {
                    h.extra.settingType = "03";
                    h.extra.powerStatus = flag2;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    String localcmd=JSON.toJSONString(h.extra);
                    if (!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if(m.flag3){
                            m.c.send(l);}
                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr4(m.ieee_ykq,m.port_ykq,localcmd);
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                }

                break;
            // 中速风
            case R.id.zhongsu_kongtiao:
                m.AnNiuId = 1;
                speed = "03";
                l1.setBackgroundResource(R.drawable.zhongsufeng);

                if (flag1) {
                    m.YuanSM.clear();
                    builder.show();
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    }


                } else {

                    h.extra.settingType = "03";
                    h.extra.powerStatus = flag2;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    String localcmd=JSON.toJSONString(h.extra);
                    if (!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if(m.flag3){
                            m.c.send(l);}
                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr4(m.ieee_ykq,m.port_ykq,localcmd);
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                }

                break;
            // 高速封
            case R.id.gaofeng_kongtiao:
                m.AnNiuId = 1;
                speed = "04";
                l1.setBackgroundResource(R.drawable.gaosufeng);
                if (flag1) {
                    m.YuanSM.clear();
                    builder.show();
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    }


                } else {
                    h.extra.settingType = "03";
                    h.extra.powerStatus = flag2;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    String localcmd=JSON.toJSONString(h.extra);
                    if (!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if(m.flag3){
                            m.c.send(l);}
                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr4(m.ieee_ykq,m.port_ykq,localcmd);
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                }

                break;
            // 温度加
            case R.id.addwd_kongtiao:
                m.AnNiuId = 1;
                Integer it = new Integer(code);
                it++;
                if(it>30){
                    it=30;
                }


                code = it + "";
                tv15.setText(code);

                if (flag1) {
                    m.YuanSM.clear();
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    }
                    builder.show();

                } else {
                    h.extra.settingType = "06";
                    h.extra.powerStatus = flag2;

                    h.extra.temperature = it + "";
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    String localcmd=JSON.toJSONString(h.extra);
                    if (!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if(m.flag3){
                            m.c.send(l);}
                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr4(m.ieee_ykq,m.port_ykq,localcmd);
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                }

                break;
            // 温度减
            case R.id.jswd_kongtiao:
                m.AnNiuId = 1;
                Integer it1 = new Integer(code);
                it1--;
                if(it1<16){
                    it1=16;
                }
                code = it1 + "";
                tv15.setText(code);
                if (flag1) {
                    m.YuanSM.clear();
                    builder.show();
                    l = JSON.toJSONString(h);
                    if (m.flag3) {
                        m.c.send(l);
                    }

                } else {
                    h.extra.settingType = "06";
                    h.extra.powerStatus = flag2;
                    h.extra.temperature = code;
                    h.extra.mode = mode;
                    h.extra.speed = speed;
                    l = JSON.toJSONString(h);
                    String localcmd=JSON.toJSONString(h.extra);
                    if (!Gateway.MacSocke.containsKey(m.mac_ykq)){
                        if(m.flag3){
                            m.c.send(l);}
                    }else{
                        String cmd = GatewayRequestUtil.createInfraredForwardingStr4(m.ieee_ykq,m.port_ykq,localcmd);
                        SendUtil.Mac = m.mac_ykq;
                        SendUtil.cmd = cmd;
                        SendUtil.hand.sendEmptyMessage(0);
                    }
                }

                break;

            case R.id.a0baocun:
                Builder builder = new AlertDialog.Builder(StudyKTActivity.this);
                builder.setTitle("请输入");
                builder.setIcon(android.R.drawable.ic_dialog_info);
                et = new EditText(StudyKTActivity.this);
                builder.setView(et);
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        HongWaiSB hs = new HongWaiSB();
                        hs.controllerList = new ArrayList<controllerList>();
                        String s1 = et.getText().toString();
                        String str2 = s1.replaceAll(" ", "");
                        if (!"".equals(str2)) {
                            hs.name = et.getText().toString();
                            hs.mac = m.mac_ykq;
                            hs.ieee=m.ieee_ykq;
                            hs.port=m.port_ykq;
                            hs.kind = m.type;
                            if (flag1) {
                                KTutil k = new KTutil();
                                for (Integer key : m.YuanSM.keySet()) {
                                    k.originalCode = m.YuanSM.get(key);
                                }
                                k.name = et.getText().toString();
                                k.switchFlg = flag2;
                                k.kind = m.type;
                                k.mac = m.mac_ykq;
                                k.ieee=m.ieee_ykq;
                                k.port=m.port_ykq;
                                k.selfLearning = 1;
                                k.speed = speed;
                                k.model = mode;
                                k.temperature = code;
                                String f = JSON.toJSONString(k);
                                f = AESUtil.encrypt(f);
                                HttpUtils httpUtils = new HttpUtils();
                                RequestParams params = new RequestParams();
                                params.setHeader("Content-Type", "application/json");
                                try {
                                    params.setBodyEntity(new StringEntity(f, "utf-8"));
                                } catch (UnsupportedEncodingException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                                httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/addArcController",
                                        params, new RequestCallBack<String>() {

                                            @Override
                                            public void onFailure(HttpException arg0, String arg1) {
                                                HttpConnection.showToast(getBaseContext(),"网络异常");
                                            }

                                            @Override
                                            public void onSuccess(ResponseInfo<String> arg0) {
                                                if (arg0.result != null && !("".equals(arg0.result))) {
                                                    arg0.result = AESUtil.decrypt(arg0.result);

                                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                                    String s = j.getString("msg");

                                                    if ("成功".equals(s)) {
                                                        m.AnNiuId = -1;
                                                        m.YuanSM.clear();
                                                        Intent it = new Intent(StudyKTActivity.this, MyYaoKongActivity.class);
                                                        startActivity(it);
                                                        finish();

                                                    } else {

                                                        HttpConnection.showToast(getBaseContext(), "服务器异常");

                                                    }

                                                } else {

                                                    HttpConnection.showToast(getBaseContext(), "服务器异常");
                                                }
                                            }

                                        });


                            } else {
                                hs.selfLearning = "0";
                                for (int i = 0; i < 9; i++) {
                                    controllerList c = new controllerList();
                                    c.button = 2 * i + 1 + "";
                                    c.num = s;
                                    hs.controllerList.add(c);
                                }

                                String k = JSON.toJSONString(hs);
                                Log.d("k333333333333333",k);
                                k = AESUtil.encrypt(k);
                                HttpUtils httpUtils = new HttpUtils();
                                RequestParams params = new RequestParams();
                                params.setHeader("Content-Type", "application/json");
                                try {
                                    params.setBodyEntity(new StringEntity(k, "utf-8"));
                                } catch (UnsupportedEncodingException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }

                                httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/addController",
                                        params, new RequestCallBack<String>() {

                                            @Override
                                            public void onFailure(HttpException arg0, String arg1) {
                                                HttpConnection.showToast(getBaseContext(),"网络异常");
                                            }

                                            @Override
                                            public void onSuccess(ResponseInfo<String> arg0) {
                                                if (arg0.result != null && !("".equals(arg0.result))) {
                                                    arg0.result = AESUtil.decrypt(arg0.result);

                                                    JSONObject j = JSONObject.parseObject(arg0.result);
                                                    String s = j.getString("msg");
                                                    if ("成功".equals(s)) {
                                                        m.YuanSM.clear();
                                                        m.AnNiuId = -1;
                                                        Intent it = new Intent(StudyKTActivity.this, MyYaoKongActivity.class);
                                                        startActivity(it);
                                                        finish();
                                                    }else{
                                                        HttpConnection.showToast(getBaseContext(),"服务器异常");
                                                    }

                                                }else{
                                                    HttpConnection.showToast(getBaseContext(),"服务器异常");
                                                }
                                            }


                                        });

                            }
                        } else {

                            HttpConnection.showToast(getBaseContext(),"网络异常");


                        }
                    }
                });


                Dialog dialog = builder.create();
                if (id == -1) {
                    dialog.show();
                } else {
                    KTutil k = new KTutil();
                    k.id = id + "";
                    k.switchFlg = flag2;
                    k.kind = m.type;
                    k.mac = m.mac_ykq;
                    k.ieee=m.ieee_ykq;
                    k.port=m.port_ykq;
                    k.selfLearning = 1;
                    k.speed = speed;
                    k.model = mode;
                    k.temperature = code;
                    k.originalCode = m.YuanSM.get(0);
                    String f = JSON.toJSONString(k);
                    f = AESUtil.encrypt(f);
                    Log.d("k444444444444444",f);
                    HttpUtils httpUtils = new HttpUtils();
                    RequestParams params = new RequestParams();
                    params.setHeader("Content-Type", "application/json");
                    try {
                        params.setBodyEntity(new StringEntity(f, "utf-8"));
                    } catch (UnsupportedEncodingException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                    httpUtils.send(HttpMethod.POST, "http://" + UserClass.ServerIP + "/FXMobiServer/remote/addArcController",
                            params, new RequestCallBack<String>() {

                                @Override
                                public void onFailure(HttpException arg0, String arg1) {
                                    HttpConnection.showToast(getBaseContext(),"网络异常");
                                }

                                @Override
                                public void onSuccess(ResponseInfo<String> arg0) {
                                    if (arg0.result != null && !("".equals(arg0.result))) {
                                        arg0.result = AESUtil.decrypt(arg0.result);
                                        JSONObject j = JSONObject.parseObject(arg0.result);
                                        String s = j.getString("msg");

                                        if ("成功".equals(s)) {
                                            m.YuanSM.clear();
                                            Intent it = new Intent(StudyKTActivity.this, MyYaoKongActivity.class);
                                            startActivity(it);
                                            finish();
                                        }else{
                                            HttpConnection.showToast(getBaseContext(),"服务器异常");
                                        }

                                    }else{
                                        HttpConnection.showToast(getBaseContext(),"服务器异常");
                                    }
                                }


                            });


                }
                break;
        }

    }

}
